package com.yf.exam.modules.trainingarchives.file.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yf.exam.ability.upload.dto.UploadReqDTO;
import com.yf.exam.ability.upload.dto.UploadRespDTO;
import com.yf.exam.ability.upload.service.UploadService;
import com.yf.exam.core.api.dto.PagingReqDTO;
import com.yf.exam.modules.trainingarchives.file.dto.FileDTO;
import com.yf.exam.modules.trainingarchives.file.entity.File;
import com.yf.exam.modules.trainingarchives.file.mapper.FileMapper;
import com.yf.exam.modules.trainingarchives.file.service.IFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.exam.modules.trainingarchives.file.vo.FileListVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 文件信息表 服务实现类
 * </p>
 *
 * @author lql
 * @since 2025-01-16
 */
@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, File> implements IFileService {

    @Resource
    UploadService uploadService;

    @Override
    public Boolean upload(FileDTO reqDTO) {
        File file=new File();
        //初始化文件信息
        file.setTrainingClassInfoId(reqDTO.getTrainingClassInfoId());
        file.setFileType(reqDTO.getFileType());
        file.setFileName(reqDTO.getFile().getOriginalFilename());
        file.setFileSize(reqDTO.getFile().getSize());
        //上传文件获取文件路径
        UploadReqDTO uploadReqDTO=new UploadReqDTO();
        uploadReqDTO.setFile(reqDTO.getFile());
        UploadRespDTO uploadRespDTO = uploadService.upload(uploadReqDTO);
        file.setFileUrl(uploadRespDTO.getUrl());
        return this.save(file);
    }

    @Override
    public IPage<FileListVO> paging(PagingReqDTO<FileListVO> reqDTO) {
        return baseMapper.paging(reqDTO.toPage(),reqDTO.getParams());
    }
}
